package com.qianfeng.missfood.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.missfood.R;
import com.qianfeng.missfood.adapter.FoodAdapter;
import com.qianfeng.missfood.bean.AppBean;
import com.qianfeng.missfood.bean.ResultListEntity;
import com.qianfeng.missfood.constants.UrlConstants;
import com.qianfeng.missfood.utils.LocationHelper;
import com.qianfeng.missfood.utils.PopupHelper;

import java.util.ArrayList;

/**
 * Created by Administrator on 15-8-17.
 */
public class FoodFragment extends BaseAppFragment implements PullToRefreshBase.OnRefreshListener2, AdapterView.OnItemClickListener, LocationHelper.BdLocationCallback, View.OnClickListener {
    @ViewInject(R.id.fragment_food_ptrlistview)
    private PullToRefreshListView ptrListView;
    @ViewInject(R.id.food_address)
    private TextView tvAddress;
    @ViewInject(R.id.food_sort_type_tv)
    private TextView sortType;
    @ViewInject(R.id.food_sort_type)
    private LinearLayout sort;
    private FoodAdapter adapter;
    private int page = 1;
    private ArrayList<ResultListEntity> list = new ArrayList<>();
    private ArrayList<String> sortString = new ArrayList<>();
    private HttpUtils httpUtils = new HttpUtils();
    private double lat;
    private double lngt;
    private String city;
    private String address;
    private String cityCode;
    ArrayAdapter<String> sortAdapter;
    private View popView;
    private PopupWindow popupWindow;

    @Override
    public int getLayoutRes() {
        return R.layout.fragment_food_layout;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        ViewUtils.inject(this, view);
        adapter = new FoodAdapter(list, getActivity(), getChildFragmentManager());
        ptrListView.setOnRefreshListener(this);
//        ptrListView.setMode(PullToRefreshBase.Mode.BOTH);
//        ptrListView.setOnItemClickListener(this);
        ptrListView.setAdapter(adapter);


        popView = LayoutInflater.from(getActivity()).inflate(R.layout.food_sort_pop, null);
        ListView popLv = (ListView) popView.findViewById(R.id.food_pop);
        sortAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, sortString);
        popLv.setAdapter(sortAdapter);
        popLv.setOnItemClickListener(this);
        popupWindow = PopupHelper.getInstance().getPopupWindow(popView);

        //设置百度地图监听器
        LocationHelper.getInstance().setBdLocationCallback(this);
        //设置排序方式监听器
        sortType.setOnClickListener(this);
//        sort.setOnClickListener(this);

        ptrListView.setRefreshing();
    }

    public void initMyData() {

        if (sortString.isEmpty()) {
            sortString.add("默认排序");
            sortString.add("按距离");
            sortString.add("按人气");
            sortString.add("全国推荐");
            sortAdapter.notifyDataSetChanged();
        }

        String url = UrlConstants.getFoodUrl(page);
        if (city != null) {
            url = UrlConstants.getFoodUrl(cityCode, page, lngt, lat, city);
        }
        httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                ptrListView.onRefreshComplete();
                String result = responseInfo.result;
                AppBean appBean = JSON.parseObject(result, AppBean.class);
                if (appBean != null) {
                    if (page == 1 && !list.isEmpty()) {
                        list.clear();
                    }

                    int i = appBean.getResultList().size();
                    list.addAll(appBean.getResultList());
                    adapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {
                ptrListView.onRefreshComplete();
            }
        });
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        page = 1;
        initMyData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        page++;
        initMyData();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.food_pop:
                sortType.setText(sortString.get(position));
                popupWindow.dismiss();
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        LocationHelper.getInstance().stop();
    }

    @Override
    public void locationCallback(double lat, double lngt, String city, String address, String cityCode) {
        tvAddress.setText(address);
        this.lat = lat;
        this.lngt = lngt;
        this.city = city;
        this.address = address;
        this.cityCode = cityCode;
    }

    @Override
    public void onClick(View v) {
        if (!popupWindow.isShowing()) {
            popupWindow.showAsDropDown(v);
        } else {
            popupWindow.dismiss();
        }
    }
}
